package com.dragon.web.commons.controller;

import com.dragon.app.commons.dto.SendSMSDTO;
import com.dragon.app.commons.service.SMSService;
import com.fintech.common.vo.FintechResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("${platform.name.code}/sms")
public class SMSController {
	@Autowired
	private SMSService smsService;
	
	@ResponseBody
	@RequestMapping(value="ykd/register",method=RequestMethod.POST)
	public FintechResult ykdRegister(@RequestBody SendSMSDTO sendSMSDTO, HttpServletRequest request){
		String ipAddress = getIPByRequest(request);
		return smsService.ykdRegister(sendSMSDTO, ipAddress);
	}
	
	@ResponseBody
	@RequestMapping(value="ykd/login",method=RequestMethod.POST)
	public FintechResult ykdLogin(@RequestBody SendSMSDTO sendSMSDTO, HttpServletRequest request){
		String ipAddress = getIPByRequest(request);
		return smsService.ykdLogin(sendSMSDTO, ipAddress);
	}
	
	@ResponseBody
	@RequestMapping(value="ykd/v2/register",method=RequestMethod.POST)
	public FintechResult ykdV2Register(@RequestBody SendSMSDTO sendSMSDTO, HttpServletRequest request){
		String ipAddress = getIPByRequest(request);
		return smsService.ykdV2Register(sendSMSDTO, ipAddress);
	}
	
	@ResponseBody
	@RequestMapping(value="ykd/v2/login",method=RequestMethod.POST)
	public FintechResult ykdV2Login(@RequestBody SendSMSDTO sendSMSDTO, HttpServletRequest request){
		String ipAddress = getIPByRequest(request);
		return smsService.ykdV2Login(sendSMSDTO, ipAddress);
	}
	
	@ResponseBody
	@RequestMapping(value="ykd/changePw",method=RequestMethod.POST)
	public FintechResult ykdchangePw(@RequestBody SendSMSDTO sendSMSDTO, HttpServletRequest request){
		String ipAddress = getIPByRequest(request);
		return smsService.ykdchangePw(sendSMSDTO, ipAddress);
	}
	
	public String getIPByRequest(HttpServletRequest request){
		String ip = "";
		if (request.getHeader("x-forwarded-for") == null) {
			ip = request.getRemoteAddr();
		}else{
			ip = request.getHeader("x-forwarded-for");
		}
		return ip;
	}
}
